package org.eggdo.apof.server;

import java.awt.Image;
import java.io.*; 
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

import org.dom4j.*;
import org.dom4j.io.*;

public class XmlParser {
	
	private String name;
	private String link;
	private String picUrl;
	private ArrayList<Item> items;
	private InputStream pic;


	public static void main(String[] args) throws FileNotFoundException, DocumentException {
		new XmlParser("abcx");
	}
	
	public XmlParser(String userName) {
		//**********************
		setProxy();
		//**********************
		
		Document itemDoc = read("http://api.fanfou.com/statuses/user_timeline.rss?id="+userName+"&count=10");
		Document userDoc = read("http://api.fanfou.com/users/show/"+userName+".xml");
		name = userDoc.selectSingleNode("user/name").getText();
		picUrl = userDoc.selectSingleNode("user/profile_image_url").getText();
		
		try{
			URLConnection imgConection = new URL(picUrl).openConnection();
			imgConection.connect();
			pic = imgConection.getInputStream();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		
		
		
		System.out.println("Name:"+name);
		System.out.println("picUrl:"+picUrl);
		
		List<Node> messages = itemDoc.selectNodes("rss/channel/item"); 
		items = new ArrayList<Item>();
		for(Iterator<Node> iter=messages.iterator();iter.hasNext();)
		{
			Node nd =(Node) iter.next();
			Item ms = new Item(nd.valueOf("title"),nd.valueOf("description"),nd.valueOf("pubDate"),nd.valueOf("link"));
			items.add(ms);
		}
	}
	

	public Document read(String fileName)  {
		Document document = null;
		try {
			SAXReader reader = new SAXReader();
			
			URL url = new URL(fileName);
			URLConnection uc = url.openConnection();
			uc.connect();
			InputStream f =uc.getInputStream();
			document = reader.read(f);

		}catch(Exception e){
			e.printStackTrace();
		}
		
		return document;
	}

	public String getTitle() {
		return name;
	}

	public void setTitle(String title) {
		this.name = title;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public ArrayList<Item> getItems() {
		return items;
	}
	
	private void setProxy() {
		Properties prop = System.getProperties();
		prop.setProperty("http.proxyHost", "192.168.1.1");
		prop.setProperty("http.proxyPort", "8080");
//		prop.setProperty("http.proxyHost", "bproxy.nju.edu.cn");
//		Authenticator.setDefault(new MyAuthenticator("MG0522005","MG0522005"));

	}
	
	class MyAuthenticator extends Authenticator {
	    private String user = "";

	    private String password = "";

	    public MyAuthenticator(String user, String password) {
	        this.user = user;
	        this.password = password;
	    }

	    protected PasswordAuthentication getPasswordAuthentication() {
	        return new PasswordAuthentication(user, password.toCharArray());
	    }

	}

	public String getPicUrl() {
		return picUrl;
	}
	
	public InputStream getImage() {
		return pic;
	}
	

}
